//
//  AdaptScreen.h
//  DHTools
//
//  Created by DreamHack on 15-9-11.
//  Copyright (c) 2015年 DreamHack. All rights reserved.
//

#ifndef DHTools_AdaptScreen_h
#define DHTools_AdaptScreen_h

#import <UIKit/UIKit.h>

#define ORIGIN_WIDTH    320
#define ORIGIN_HEIGHT   568
#define SCREEN_SIZE     [UIScreen mainScreen].bounds.size

#define DH_INLINE   static inline

DH_INLINE CGFloat DHVerticalMultiplier()
{
    return SCREEN_SIZE.height/ORIGIN_HEIGHT;
}

DH_INLINE CGFloat DHHorizontalMultiplier()
{
    return SCREEN_SIZE.width/ORIGIN_WIDTH;
}

DH_INLINE CGPoint DHFlexibleCenter(CGPoint iphone5Center)
{
    CGFloat x = iphone5Center.x * DHHorizontalMultiplier();
    CGFloat y = iphone5Center.y * DHVerticalMultiplier();
    return CGPointMake(x, y);
}

DH_INLINE CGSize DHFlexibleSize(CGSize iphone5Size, BOOL adjustWidth)
{
    CGFloat width = iphone5Size.width * DHHorizontalMultiplier();
    if (adjustWidth) {
        width = iphone5Size.width * DHVerticalMultiplier();
    }
    CGFloat height = iphone5Size.height * DHVerticalMultiplier();
    return CGSizeMake(width, height);
}

DH_INLINE CGRect DHFrameWithCenterAndSize(CGPoint center, CGSize size)
{
    CGRect frame;
    frame.origin.x = center.x - size.width/2;
    frame.origin.y = center.y - size.height/2;
    frame.size = size;
    return frame;
}

DH_INLINE CGPoint DHCenterFromFrame(CGRect frame)
{
    CGPoint center;
    center.x = frame.origin.x+frame.size.width/2;
    center.y = frame.origin.y+frame.size.height/2;
    return center;
}

DH_INLINE CGRect DHFlexibleFrame(CGRect iphone5Frame, BOOL adjustWidth)
{
    // 拿到iphone5 frame的center
    CGPoint center = DHCenterFromFrame(iphone5Frame);
    // 对center进行等比例缩放
    CGPoint flexibleCenter = DHFlexibleCenter(center);
    // 对size进行等比例缩放
    CGSize flexibleSize = DHFlexibleSize(iphone5Frame.size, adjustWidth);
    // 通过center和size合成frame
    return DHFrameWithCenterAndSize(flexibleCenter, flexibleSize);
}

#endif
